package com.lisp.algorithm.sort;

import com.lisp.algorithm.util.SortUtil;

public class SelectionSort {

    public static void main(String[] args) {
        SortUtil.test(SelectionSort::sort);
        SortUtil.test(SelectionSort::selectionSort);
    }
    
    private static void sort(int a[]) {
        for(int sortedIndex=0; sortedIndex<a.length-1; sortedIndex++) {
            int currentMinValueIdx = sortedIndex, currentMinValue = a[sortedIndex];
            for(int testIdx=sortedIndex+1; testIdx < a.length; testIdx++) {
                if(currentMinValue > a[testIdx]) {   // 
                    currentMinValue = a[testIdx];
                    currentMinValueIdx = testIdx;
                }
            }
            a[currentMinValueIdx] = a[sortedIndex];
            a[sortedIndex] = currentMinValue;
        }
    }
    
    private static void selectionSort(int a[]) {
        for(int round=1; round<a.length; round++) {
            int max = a[0];
            int maxOriginIdx = 0;
            int j = 1;
            for(; j<=a.length-round; j++) {
                if(a[j] > max) {
                    max = a[j];
                    maxOriginIdx = j;
                }
            }
            a[maxOriginIdx] = a[j-1];
            a[j-1] = max;
        }
    }
}
